// pages/index/mediationroom/mediationroom.js
const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
const http = new app.appHttp()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        //电话咨询  名字
        phoneName: '',
        //电话咨询  电话
        phonePhone: '',
        phoneShow: false,
        show: false,
        TextValue: "",
        index: "",
        id: "",
        title: "",
        isLoading: true,
        HaveInHandList: [],
        FinishedList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            theme: app.globalData.theme
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getReserveList()
        this.getMediationList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 当前激活的标签改变时触发
     * @param event
     */
    onChange(event) {
        const than = this
        const num = event.detail.index
        this.setData({
            active: num
        })
        if (num == 0) {
            this.getReserveList()
        } else if (num == 1) {
            than.getMediationList()
        } else if (num == 2) {
            than.getNoMediationList(2)
        }
    },
    /**
     * 显示输入房间码对话框
     */
    onContradictionDetails: function (e) {
        const than = this
        const name = wx.getStorageSync("name")
        const phone = wx.getStorageSync("phone")
        const index = e.currentTarget.dataset.index
        than.setData({
            id: e.currentTarget.dataset.id,
            title: e.currentTarget.dataset.title,
            index: e.currentTarget.dataset.index,
            status: e.currentTarget.dataset.status,//0关闭1未关闭
            mediate_code: e.currentTarget.dataset.mediate_code,//房间码
        })
        let data = this.data.HaveInHandList[index]
        log.e(than.data.mediate_code)
        if (data.in_status == 1) {
            log.e(name+phone)
            if (name.length <= 0 || phone.length <= 0) {
                this.setData({
                    phoneShow: true
                })
            } else {
                http.postJson("mediate_information_user/add", {
                    name: name,
                    sex: app.globalData.userInfo.sex,
                    type: "1",
                    phone: phone,
                    mediate_information_id: than.data.id,
                    applet_user_id: app.globalData.userInfo.applet_user_id,
                    in_type: "1"
                }).then(res => {
                    wx.navigateTo({
                        url: "../mediationroom/contradictiondetails/contradictiondetails?id=" + than.data.id + '&title=' + than.data.title
                    })
                })
            }
        } else {
            than.setData({
                show: true
            })
        }
    },
    onConfirm() {
        const than = this
        const name = wx.getStorageSync("name")
        const phone = wx.getStorageSync("phone")
        log.e("onConfirm")
        than.setData({
            show: false,
        })
        if (than.data.TextValue == than.data.mediate_code) {
            if (name.length <= 0 || phone.length <= 0) {
                than.setData({
                    phoneShow: true,
                })
            }else{
                wx.setStorageSync(than.data.id, than.data.mediate_code);
                wx.navigateTo({
                    url: "../mediationroom/contradictiondetails/contradictiondetails?id=" + than.data.id + '&title=' + than.data.title
                })
                than.setData({
                    TextValue: ""
                })
            }
        
        } else {
            Toast.fail('访问码错误');
            than.setData({
                TextValue: ""
            })
        }


    },
    onClose() {
        this.setData({
            close: false,
        });
    },
    getTextValue: function (e) {
        this.setData({
            TextValue: e.detail.value,
        })
    },

    /**
     * 预约列表下拉刷新
     * @param e
     */
    onRefresh: function (e) {
        this.setData({
            isRefresh: true,
            reserveList: []
        })
        this.getReserveList()
    },
    /**
     * 获取预约列表
     * @param e
     */
    getReserveList: function (e) {
        this.setData({
            isLoading: true
        })
        http.get(app.globalData.xw_api_url + "mediate/information/getSelect", {
            reply_user_id: app.globalData.userInfo.applet_user_id,
            area_code: app.globalData.areaCode,
            status: 1,
            is_make: 1,
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                this.setData({
                    reserveList: res.data.data
                })
            } else {
                Toast.fail(res.data.msg)
            }
            this.setData({
                isRefresh: false,
                isLoading: false
            })
        }, err => {
            this.setData({
                isRefresh: false,
                isLoading: false
            })
        })
    },
    /**
     * 获取进行中聊天室列表
     * @param e
     */
    getMediationList: function (e) {
        this.setData({
            isLoading: true
        })
        http.get(app.globalData.xw_api_url + "mediate/information/getSelect", {
            reply_user_id: app.globalData.userInfo.applet_user_id,
            area_code: app.globalData.areaCode,
            status: 1,
            is_make: 0,
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                this.setData({
                    HaveInHandList: res.data.data
                })
            } else {
                Toast.fail(res.data.msg)
            }
            this.setData({
                isLoading: false
            })
        }, err => {
            this.setData({
                isLoading: false
            })
        })
    },
    /**
     * 获取结束列表
     * @param e
     */
    getNoMediationList: function (e) {
        this.setData({
            isLoading: true
        })
        http.get(app.globalData.xw_api_url + "mediate/information/getSelect", {
            reply_user_id: app.globalData.userInfo.applet_user_id,
            area_code: app.globalData.areaCode,
            status: 0,
            is_make: 0,
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                this.setData({
                    FinishedList: res.data.data
                })
            } else {
                Toast.fail(res.data.msg)
            }
            this.setData({
                isLoading: false
            })
        }, err => {
            this.setData({
                isLoading: false
            })
        })
    },
    /**
 * 电话咨询 dialog 关闭
 * @param e
 */
    onPhoneClose: function (e) {
        this.setData({
            phoneShow: false,
            phoneName: '',
            phonePhone: '',
        })
    },
    /**
   * 电话咨询 dialog 确定
   * @param e
   */
    onPhoneConfirm: function (e) {
        const than=this
        if (this.data.phoneName.length <= 0) {
            Toast.fail("姓名不能为空")
            return false
        }
        if (this.data.phonePhone.length <= 0) {
            Toast.fail("联系电话不能为空")
            return false
        }
        if (this.data.phonePhone.length < 11) {
            Toast.fail("请输入正确的联系电话")
            return false
        }
        try {
            wx.setStorageSync("name", this.data.phoneName)
            wx.setStorageSync("phone", this.data.phonePhone)
            wx.setStorageSync(than.data.id, than.data.mediate_code);
        } catch (e) { }
        this.setData({
            phoneShow: false
        })
        wx.navigateTo({
            url: "../mediationroom/contradictiondetails/contradictiondetails?id=" + than.data.id + '&title=' + than.data.title
        })
        than.setData({
            TextValue: ""
        })
    },
    /**
   * 电话咨询 姓名输入框实时监听
   * @param e
   */
    onInputPhoneName: function (e) {
        log.e(e.detail.value)
        this.setData({
            phoneName: e.detail.value
        })
    },
    /**
     * 电话咨询 联系电话输入框实时监听
     * @param e
     */
    onInputPhonePhone: function (e) {
        log.e(e.detail.value)
        this.setData({
            phonePhone: e.detail.value
        })
    },

    /**
     * 新增预约
     * @param e
     */
    onAddReserve: function (e) {
        wx.navigateTo({
            url: "./add_reserve/add_reserve"
        })
    },

    /**
     * 预约详情
     * @param e
     */
    onReserveDetails: function (e) {
        const index = e.currentTarget.dataset.index
        let detail = this.data.reserveList[index]
        if (detail.make_status == 2) {
            this.setData({
                active: 1
            })
        }
    },
})